package com.woniuxy.woniuticket.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;

import com.woniuxy.woniuticket.realm.UserRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShiroConfig {

    /**
     * 配置Shiro过滤器。指定哪些资源需要进行认证后才能访问。
     * @return
     */
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition(){
        DefaultShiroFilterChainDefinition defaultShiroFilterChainDefinition = new DefaultShiroFilterChainDefinition();
        defaultShiroFilterChainDefinition.addPathDefinition("/moviemanager/login.html","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/movieshow/choose_seat.html?id=/","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/moviemanager/login","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/cinemaAdmin/adminLogin","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/cinemaAdmin/checkCode","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/movie/findAll","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/movie/findMovieById","anon");

        defaultShiroFilterChainDefinition.addPathDefinition("/schedule/findSchedule","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/schedule/findScheduleE","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/schedule/findScheduleET","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/schedule/selectByScheduleId/","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/schedule/msg","anon");

        defaultShiroFilterChainDefinition.addPathDefinition("/cinemaInfo/select/","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/sear/moveis","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/sear/showmovies","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/seats/selectBySid","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/wsMy","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/hall/selectById","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/movie/selectOne","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/login/logout","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/vip/showVip","anon");

        defaultShiroFilterChainDefinition.addPathDefinition("/zilanxuan/**","anon");

        defaultShiroFilterChainDefinition.addPathDefinition("/movieshow/**","anon");

        /*订单*/
        defaultShiroFilterChainDefinition.addPathDefinition("/order/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/comment/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/orderwebsocket/**","anon");

        defaultShiroFilterChainDefinition.addPathDefinition("/css/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/movies/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/upload/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/evan/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/font/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/image/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/images/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/js/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/managerstatic/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/showstatic/**","anon");

        defaultShiroFilterChainDefinition.addPathDefinition("/login","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/regist","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/getCode","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/wsMy","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/msg","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/schedule/showschedule1","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/schedule/msg","anon");


        //订单放行
        defaultShiroFilterChainDefinition.addPathDefinition("/order/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/comment/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/orderwebsocket/**","anon");


        defaultShiroFilterChainDefinition.addPathDefinition("/login/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/userinfo/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/problem/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/login/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/zilanxuan/**","anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/openapi.alipaydev.com/gateway.do","anon");

        defaultShiroFilterChainDefinition.addPathDefinition("/logout","logout");
        defaultShiroFilterChainDefinition.addPathDefinition("/**","authc");
        return defaultShiroFilterChainDefinition;
    }

    /**
     * 创建自定义域对象，注入到IOC容器中
     * @param matcher 域对象使用的密码匹配器 ，默认情况下Spring工厂（容器）会根据bean的类型完成自动注入
     * @param constrainedCacheManager 缓存管理器
     * @return
     */
    @Bean(value="authorizer")
    public UserRealm customRealm(HashedCredentialsMatcher matcher, MemoryConstrainedCacheManager constrainedCacheManager){
        UserRealm userRealm = new UserRealm();
        userRealm.setCredentialsMatcher(matcher);
        userRealm.setCacheManager(constrainedCacheManager);
        return userRealm;
    }


//    @Bean
//    public DefaultWebSecurityManager securityManager(CustomRealm customRealm){
//        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        return securityManager;
//    }

    /**
     * 创建密码匹配器对象
     * @return
     */
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("md5");
        matcher.setHashIterations(2);
        return matcher;
    }

    /**
     * 缓存管理器对象
     * @return
     */
    @Bean(value = "memoryConstrainedCacheManager")
    public MemoryConstrainedCacheManager cacheManager(){
        return new MemoryConstrainedCacheManager();
    }


    /**
     * thymeleaf中使用shiro标签
     * @return
     */
    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }















}
